package com.itqf.mybatis;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itqf.bean.User;
import com.itqf.mapper.UserMapper;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.InputStream;
import java.util.List;

/**
 * projectName: mybatis2103
 *
 * @author: 赵伟风
 * time: 2021/6/24 10:14
 * description:
 */
public class UseMybatisOneCache {

    public static void main(String[] args) throws Exception{


        //1.读取配置
        InputStream stream = Resources.getResourceAsStream("mybatis_config.xml");
        //2.创建sqlsesssionfactorybuilder
        SqlSessionFactoryBuilder factoryBuilder = new SqlSessionFactoryBuilder();
        //3.创建sqlsessionfactory
        SqlSessionFactory sqlSessionFactory = factoryBuilder.build(stream);
        //4.创建sqlsession
        SqlSession sqlSession = sqlSessionFactory.openSession();

        //5.获取mapper
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);

        //设置分页参数
        /**
         * 参数1:PageNum 指定页数
         * 参数2:PageSize 每页条数
         */
        PageHelper.startPage(2, 1);

        //查询
        List<User> users = mapper.queryAll();

        //封装PageInfo
        PageInfo<User> pageInfo = new PageInfo<>(users);


        //获取数据
        //当前页数
        int pageNum = pageInfo.getPageNum();
        System.out.println("pageNum = " + pageNum);
        //页容量
        int pageSize = pageInfo.getPageSize();
        System.out.println("pageSize = " + pageSize);
        //总页数
        int pages = pageInfo.getPages();
        System.out.println("pages = " + pages);
        //总条数
        long total = pageInfo.getTotal();
        System.out.println("total = " + total);
        //当前页的数据
        List<User> list = pageInfo.getList();
        System.out.println("list = " + list);


    }
}
